package com.crm.action;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.crm.dao.PaperDao;
import com.crm.dao.QuestionDao;
import com.crm.model.business.Paper;
import com.crm.model.business.Question;
import com.crm.model.role.Student;
import com.crm.util.ResponseUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author: GaoXu
 * @date: 2020/7/22 22:57
 * @Description: (用一句话描述该文件做什么)
 */
public class PaperAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 1L;
    private HttpServletRequest request;
    private final PaperDao paperDao = new PaperDao();
    private QuestionDao questionDao = new QuestionDao();
    private String mainPage;
    private List<Paper> paperList = new ArrayList<Paper>();
    private String id;
    private Paper paper;
    private List<Question> singleQuestionList = new ArrayList<Question>();
    private List<Question> multipleQuestionList = new ArrayList<Question>();
    private String title;


    public String list() {
        HttpSession session = request.getSession();
        Object currentUser = session.getAttribute("currentUser");
        if (currentUser instanceof Student) {
            mainPage = "business/paper/selectPaper.jsp";
        } else {
            mainPage = "business/paper/paperList.jsp";
        }
        paperList = paperDao.list();
        return SUCCESS;
    }

    public String getPaperById() {
        paper = paperDao.getPaperById(id);
        List<Question> questionList = paper.getQuestionList();
        for (Question question : questionList) {
            if ("1".equals(question.getType())) {
                singleQuestionList.add(question);
            } else {
                multipleQuestionList.add(question);
            }
        }
        mainPage = "business/paper/paper.jsp";
        return SUCCESS;
    }

    public String preSave() {
        if (ObjectUtils.isNotEmpty(id)) {
            paper = paperDao.getPaperById(id);
            title = "修改试卷";
        } else {
            title = "添加试卷";
        }
        mainPage = "business/paper/paperSave.jsp";
        return SUCCESS;
    }

    public String save() {
        if (ObjectUtils.isNotEmpty(id)) {
            paper.setId(Integer.parseInt(id));
        } else {
            paper.setCreateTime(new Date());
        }
        paperDao.save(paper);
        return "save";
    }

    public void delete(){
        paper = paperDao.getPaperById(id);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (paper.getQuestionList().size() > 0) {
            resultMap.put("error", "试卷下面有题目，不能删除");
        } else {
            paperDao.delete(paper);
            resultMap.put("success", true);
        }
        ResponseUtil.write(JSON.toJSONString(resultMap) , ServletActionContext.getResponse());
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String getMainPage() {
        return mainPage;
    }

    public void setMainPage(String mainPage) {
        this.mainPage = mainPage;
    }

    public List<Paper> getPaperList() {
        return paperList;
    }

    public void setPaperList(List<Paper> paperList) {
        this.paperList = paperList;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }

    public List<Question> getSingleQuestionList() {
        return singleQuestionList;
    }

    public void setSingleQuestionList(List<Question> singleQuestionList) {
        this.singleQuestionList = singleQuestionList;
    }

    public List<Question> getMultipleQuestionList() {
        return multipleQuestionList;
    }

    public void setMultipleQuestionList(List<Question> multipleQuestionList) {
        this.multipleQuestionList = multipleQuestionList;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
